﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DrugStore.Models;
using DrugStore.Exceptions;

namespace DrugStore.Models
{
    public class DiseaseModel
    {
         public String DiseaseId{get;set;}
         public String DiseaseCategoryId { get; set; }
         public String ProductId { get; set; }
         public String DiseaseName { get; set; }
         public String BriefDesc { get; set; }
         public String ImgPath { get; set; }
         public int Status { get; set; }
         public String Content { get; set; }

         public List<News> NewList { get; set; }
         public DiseaseModel(Disease disease) 
         {
             DiseaseId = disease.DiseaseId.ToString();
             DiseaseCategoryId = disease.DiseaseCategoryId.ToString();
             ProductId = disease.ProductId.ToString();
             DiseaseName = disease.DiesaseName;
             BriefDesc = disease.BriefDesc;
             ImgPath = disease.ImgPath;
             Status = disease.Status;
             Content = disease.Content;
             NewList = new List<News>();

             //Create NewBussiness object.
             NewBussiness newBussiness = new NewBussiness();
             List<News> news = new List<News>();
             var categories = disease.Categories;
             foreach (Category c in categories)
             {
                 news.AddRange((from n in c.News
                          orderby n.DateCreate descending
                          select n).Take(10));
             }

             if (news.Count > 3)
             {
                 while (NewList.Count < 3)
                 {
                     Random ran = new Random();
                     int index = ran.Next() % news.Count;
                     if (!NewList.Contains(news[index]))
                     {
                         NewList.Add(news[index]);
                     }
                 }
             }
             else
             {
                 NewList = news;
             }

             //Create a counter object to sure that get just three news.
             //int flag = 0;
             //Get three disease's news.             
             //foreach (News_Disease newDisease in disease.News_Disease) 
             //{
             //    //Create a temp new.
             //    News tempNew = newBussiness.GetNewsById(newDisease.NewId.ToString().Trim());
             //    NewList.Add(tempNew);
                 
             //    //Check whether the number of news that equal 3.
             //    if (flag == 2)
             //    {
             //        //get out of for loop.
             //        break;
             //    }
             //    //Increment the value of variable flag.
             //    flag++;                                
             //}
         }
    }

    public class DiseaseService 
    {
        DrugStoreEntities entities;

        /*
         * Select one disease
         * Parameter is id
         * Return type is Disease Model.
         * 
         */                
        public DiseaseModel SelectDisease(String id)
        {
            Guid diseaseId = new Guid();
            if(!Guid.TryParse(id,out diseaseId))
            {
                throw new ParameterInvalidException("Error404", "404");
            }
            entities = new DrugStoreEntities();
            Disease temp = entities.Diseases.FirstOrDefault(di => di.DiseaseId.Equals(diseaseId));
            if (temp == null) 
            {
                throw new ParameterInvalidException("Null", "Don't exist product");
            }           
            DiseaseModel disease = new DiseaseModel(temp);
            return disease;            
        }
        
        //Create new Disease instance.
        //return type: int
        //Parameter DiseaseModel
        public int CreateDisease(DiseaseModel model) 
        {
            return 0;
        }

        /*Update Disease
         * return type int
         * Parameter DiesaseModel         
         */
        public int UpdateDiesase(DiseaseModel model)
        {
            return 0;
        }
        
    }
        
}